package com.zzld.modules.autoinn.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.abel533.entity.EntityMapper;
import com.github.abel533.entity.Example;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzld.core.service.BaseServiceImpl;
import com.zzld.modules.autoinn.entity.Store;
import com.zzld.modules.autoinn.mapper.StoreMapper;
import com.zzld.modules.autoinn.service.IStoreService;
import com.zzld.modules.common.dto.TreeGrid;

@Service("storeService")
public class StoreServiceImpl extends BaseServiceImpl<Store>  implements IStoreService {

	@Autowired
	private StoreMapper storeMapper;

	@Autowired
	private EntityMapper entityMapper;

	
	/**
	 * 根据距离获取分页
	 * @param map
	 * @param PageNum
	 * @param PageSize
	 * @return
	 */
	public PageInfo<Store> selectOrderDistance(Map<String,Object> map,int PageNum,int PageSize){
		PageHelper.startPage(PageNum, PageSize);
		List<Store> list = storeMapper.selectByDistance(map);
		PageInfo<Store> page = new PageInfo<Store>(list);
		return page;
	}
	
	public PageInfo<Store> selectOrderSold(Map<String,Object> map,int PageNum,int PageSize){
		PageHelper.startPage(PageNum, PageSize);
		List<Store> list = storeMapper.selectOrderSold(map);
		PageInfo<Store> page = new PageInfo<Store>(list);
		return page;
	}
	
	public PageInfo<Store> selectOrderprice(Map<String,Object> map,int PageNum,int PageSize){
		PageHelper.startPage(PageNum, PageSize);
		List<Store> list = storeMapper.selectOrderprice(map);
		PageInfo<Store> page = new PageInfo<Store>(list);
		return page;
	}
	
	public PageInfo<Store> selectOrderfen(Map<String,Object> map,int PageNum,int PageSize){
		PageHelper.startPage(PageNum,PageSize);
		List<Store> list = storeMapper.selectOrderfen(map);
		PageInfo<Store> page = new PageInfo<Store>(list);
		return page;
	}

	/**
	 * 分页查询
	 * 
	 * @param map
	 * @param PageNum
	 * @param PageSize
	 * @return
	 */
	public PageInfo selectByPage(Map map, int PageNum, int PageSize) {
		Map<String, Object> params = map;
		// 以name字段升序排序
		PageHelper.startPage(PageNum, PageSize);
		List<Store> list = storeMapper.selectByPage(map);
		PageInfo page = new PageInfo(list);

		return page;
	}
	
	public Integer selectprocount(String id){
		return (Integer)storeMapper.selectprocount(id);
	}

	/**
	 * 删除操作
	 * 
	 * @param id
	 * @return
	 */
	public int deletebykey(String id) {
		try {
			return (Integer) storeMapper.deleteByPrimaryKey(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 修改/添加操作
	 * 
	 * @param store
	 * @param i
	 * @return
	 */
	public int upOraddstore(Store store, int i) {
		if (store != null) {
			try {
				if (i == 0) {
					return (Integer) storeMapper.upbyid(store);
				} else {
					return (Integer) storeMapper.inseby(store);
				}

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return 0;
	}

	/**
	 * 根据id查询一条数据
	 * 
	 * @param id
	 * @return
	 */ 
	public HashMap<String, Object> selectbykey(String id) {
       

		try {
			 return storeMapper.selectbyid(id);
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public HashMap<String, Object> selecthoukey(String id) {
		return storeMapper.selecthoukey(id);
	}

	public HashMap<String, Object> selectkey(String id) {
		try {
			return storeMapper.selectkey(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取树形网格
	 * 
	 * @return
	 */
	public List<TreeGrid> treeGrid() {
		List<TreeGrid> treeGrids = new ArrayList<TreeGrid>();
		Example e = new Example(Store.class);
		List<Store> listRes = storeMapper.selectbytregid();
		if (listRes != null && listRes.size() > 0) {
			for (Store res : listRes) {
				TreeGrid treeGrid = new TreeGrid();
				BeanUtils.copyProperties(res, treeGrid);
				treeGrid.setId(res.getId());
				treeGrid.setText(res.getName());
				treeGrids.add(treeGrid);
			}
		}
		return treeGrids;
	}

	public List<Store> selectbyprice(String id) {
		return storeMapper.selectbyprice(id);
	}

	@Override
	public PageInfo<Store> selectbybyfen(Map<String,Object> map,int PageNum,int PageSize) {
		// TODO 自动生成的方法存根
		PageHelper.startPage(PageNum, PageSize);
		List<Store> list = storeMapper.selectbybyfen(map);
		PageInfo<Store> page = new PageInfo<Store>(list);
		return page;
	
	}

	@Override
	public PageInfo<Store> selectbybyjuli(Map<String,Object> map,int PageNum,int PageSize) {
		// TODO 自动生成的方法存根
		PageHelper.startPage(PageNum, PageSize);
		List<Store> list = storeMapper.selectbybyjuli(map);
		PageInfo<Store> page = new PageInfo<Store>(list);
		return page;
	}
}
